/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ronsicecream.pos.wh.transaction.receiptfromsupplierdetail;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Controller;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Logger;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.buanalintas.bynga.Utility;
import com.buanalintas.bynga.formatter.DateFormatter;
import com.ronsicecream.pos.entities.PosTransactionHoPurchaseOrderSupplierDetail;
import com.ronsicecream.pos.entities.PosTransactionHoReceiptFromSupplier;
import com.ronsicecream.pos.entities.PosTransactionHoReceiptFromSupplierDetail;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author user
 */
public class ReceiptFromSupplierDetail extends Page {

    private Field receiptDetailId;
    private Field receiptDetailQuantity;
    private Field receiptDetailPrice;
    private Field receiptDetailDescription;
    private Field receiptDetailReceiptId;
//    private Field receiptDetailSatuanId;
    private Field receiptDetailItemId;
    private Field receiptDetailDateExpired;
    private Field receiptDetailDateReceipt;
    private Field receiptDetailBarcode;

    public ReceiptFromSupplierDetail() {
        super();

        setTheClass(PosTransactionHoReceiptFromSupplierDetail.class);




        receiptDetailId = new Field();
        receiptDetailId.setName("receiptDetailId");
        receiptDetailId.setCaption(" id");
        //receiptDetailId.setInfo("receipt detail id");
        receiptDetailId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        receiptDetailId.setInputProperties(" size=\"20\"");
        getFields().add(receiptDetailId);

        receiptDetailBarcode = new Field();
        receiptDetailBarcode.setName("receiptDetailBarcode");
        receiptDetailBarcode.setCaption("barcode");
        receiptDetailBarcode.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        receiptDetailBarcode.setInputProperties(" size=\"10\" style=\"text-align: right;\" readonly");
        receiptDetailBarcode.setInputCase(Field.CASE_UPPER);
        getFields().add(receiptDetailBarcode);

        receiptDetailQuantity = new Field();
        receiptDetailQuantity.setName("receiptDetailQuantity");
        receiptDetailQuantity.setCaption(" quantity");
        receiptDetailQuantity.setRequired(true);
        receiptDetailQuantity.setListProperties(" align=\"left\" width=\"5%\" nowrap");
        receiptDetailQuantity.setInputProperties(" size=\"20\"");
        getFields().add(receiptDetailQuantity);

        receiptDetailPrice = new Field();
        receiptDetailPrice.setName("receiptDetailPrice");
        receiptDetailPrice.setCaption(" price");
        receiptDetailPrice.setRequired(true);
        receiptDetailPrice.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        receiptDetailPrice.setInputProperties(" size=\"20\"");
        getFields().add(receiptDetailPrice);

        receiptDetailDescription = new Field();
        receiptDetailDescription.setName("receiptDetailDescription");
        receiptDetailDescription.setCaption("description");
        //receiptDetailDescription.setInfo("receipt detail description");
        receiptDetailDescription.setListProperties(" align=\"left\" width=\"30%\" nowrap");
        receiptDetailDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        receiptDetailDescription.setInputType(Field.INPUT_TEXT_AREA);
        receiptDetailDescription.setInputCase(Field.CASE_UPPER);
        getFields().add(receiptDetailDescription);

        receiptDetailReceiptId = new Field();
        receiptDetailReceiptId.setName("receiptDetailReceiptId");
        receiptDetailReceiptId.setCaption("supplier");
        //posTransactionReceiptFromSupplier.setInfo("pos transaction receipt from supplier");
        receiptDetailReceiptId.setListProperties(" align=\"left\" width=\"20%\" nowrap");
        receiptDetailReceiptId.setInputProperties(" size=\"20\"");
        getFields().add(receiptDetailReceiptId);

        receiptDetailDateReceipt = new Field();
        receiptDetailDateReceipt.setName("receiptDetailDateReceipt");
        receiptDetailDateReceipt.setCaption("Date Receipt");
        receiptDetailDateReceipt.setRequired(true);
        receiptDetailDateReceipt.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        receiptDetailDateReceipt.setInputProperties(" size=\"20\"");
        receiptDetailDateReceipt.setDefaultValue(Utility.getFirstDate());
        getFields().add(receiptDetailDateReceipt);

        receiptDetailDateExpired = new Field();
        receiptDetailDateExpired.setName("receiptDetailDateExpired");
        receiptDetailDateExpired.setCaption("Date Expired");
        receiptDetailDateExpired.setRequired(true);
        receiptDetailDateExpired.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        receiptDetailDateExpired.setInputProperties(" size=\"20\"");
        receiptDetailDateExpired.setDefaultValue(Utility.getLastDate());
        getFields().add(receiptDetailDateExpired);

        receiptDetailItemId = new Field();
        receiptDetailItemId.setName("receiptDetailItemId");
        receiptDetailItemId.setCaption("item");
        receiptDetailItemId.setRequired(true);
        receiptDetailItemId.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemName", " Nama", "width=\"40%\""});
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemCode", " Code", "width=\"20%\" nowrap"});
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemTypeId", " Type", "width=\"20%\""});  
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemMinimum", " Minimum", "width=\"20%\""}); 
        receiptDetailItemId.setSelectorOrder("t.itemName");
        getFields().add(receiptDetailItemId);


        fillFields();
        getFieldsAdd().clear();
        getFieldsAdd().add(receiptDetailItemId);
//        getFieldsAdd().add(receiptDetailBarcode);
        getFieldsAdd().add(receiptDetailQuantity);
        getFieldsAdd().add(receiptDetailDateReceipt);
        getFieldsAdd().add(receiptDetailDateExpired);
//        getFieldsAdd().add(receiptDetailSatuanId);
        getFieldsAdd().add(receiptDetailPrice);
        getFieldsAdd().add(receiptDetailDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(receiptDetailItemId);
//        getFieldsEdit().add(receiptDetailBarcode);
        getFieldsEdit().add(receiptDetailQuantity);
        getFieldsEdit().add(receiptDetailDateReceipt);
        getFieldsEdit().add(receiptDetailDateExpired);
        getFieldsEdit().add(receiptDetailPrice);
        getFieldsEdit().add(receiptDetailDescription);

        getFieldsList().clear();
        getFieldsList().add(receiptDetailItemId);
        getFieldsList().add(receiptDetailBarcode);
        getFieldsList().add(receiptDetailQuantity);
        getFieldsList().add(receiptDetailDateReceipt);
        getFieldsList().add(receiptDetailDateExpired);
        getFieldsList().add(receiptDetailPrice);
        getFieldsList().add(receiptDetailDescription);

        getFieldsView().clear();
        getFieldsView().add(receiptDetailItemId);
        getFieldsView().add(receiptDetailBarcode);
        getFieldsView().add(receiptDetailQuantity);
        getFieldsView().add(receiptDetailDateReceipt);
        getFieldsView().add(receiptDetailDateExpired);
        getFieldsView().add(receiptDetailPrice);
        getFieldsView().add(receiptDetailDescription);


        getActions().add(Action.TRANS_DETAIL_LIST);
        getActions().add(Action.TRANS_DETAIL_ADD);
        getActions().add(Action.TRANS_DETAIL_EDIT);
    }

    @Override
    public void afterInit(HttpServletRequest req, HttpServletResponse res) {
        receiptDetailDateExpired.setFormater(DateFormatter.getInstanceDate(this));
        receiptDetailDateReceipt.setFormater(DateFormatter.getInstanceDate(this));
    }

    @Override
    public void loadDetail(List datas, String id) {
        try {
            EntityManager em = getEntityManager();
            List<PosTransactionHoPurchaseOrderSupplierDetail> request = em.createQuery("SELECT t FROM PosTransactionHoPurchaseOrderSupplierDetail t "
                    + " WHERE t.purchaseOrderDetailPurchaseOrderId.purchaseOrderId=" + id.toString())
                    .setHint(Persistence.REFRESH_HINT, true).getResultList();
            datas.clear();
            for (PosTransactionHoPurchaseOrderSupplierDetail pd : request) {
                PosTransactionHoReceiptFromSupplierDetail rd = new PosTransactionHoReceiptFromSupplierDetail();
                rd.setReceiptDetailItemId(pd.getPurchaseOrderDetailItemId());
                rd.setReceiptDetailDateReceipt(Utility.getTimestamp());
//                rd.setReceiptDetailDateExpired(rd.getReceiptDetailDateReceipt());
                rd.setReceiptDetailQuantity(pd.getPurchaseOrderDetailUnsent());
                System.out.println("Quantity " + rd.getReceiptDetailQuantity());
                rd.setReceiptDetailPrice(pd.getPurchaseOrderDetailPrice());
//                rd.setReceiptDetailSatuanId(pd.getPurchaseOrderDetailSatuanId());
                rd.setReceiptDetailDescription(pd.getPurchaseOrderDetailDescription());
                if ((rd.getReceiptDetailQuantity().intValue()) > 0) {
                    datas.add(rd);
                }
            }
        } catch (Exception e) {
            Logger.getInstance().log(e);
            System.out.println("Errrorr " + e);
        }
    }
}
